View Javadoc
1 //============================================================================== 2 // file : JDOUtil.java 3 // project: Java Common Utility 4 // 5 // last change: date: $Date: 2003/09/10 09:22:14 $ 6 // by: $Author: bitiboy $ 7 // revision: $Revision: 1.1 $ 8 //------------------------------------------------------------------------------ 9 // copyright: GNU GPL Software License (see class documentation) 10 //============================================================================== 11 package com.justhis.jdo; 12 13 14 /* 15 * $Id: JDOUtil.java,v 1.1 2003/09/10 09:22:14 bitiboy Exp $ 16 * 17 * Copyright 2003 Acai Software All Rights Reserved. 18 * 19 * This file JDOUtil.java is part of the Java Common Utility. 20 21 * The Java Common Utility is free software; you can redistribute it and/or modify 22 * it under the terms of the GNU General Public License as published by 23 * the Free Software Foundation; either version 2 of the License, or 24 * (at your option) any later version. 25 26 * Java Common Utility is distributed in the hope that it will be useful, 27 * but WITHOUT ANY WARRANTY; without even the implied warranty of 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 29 * GNU General Public License for more details. 30 31 * You should have received a copy of the GNU General Public License 32 * along with the Java Common Utility; if not, write to the Free Software 33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 34 35 * http://www.justhis.com http://ejb.cn 36 * CONTACT: email = webmaster@justhis.com superaxis@sohu.com 37 */ 38 import com.justhis.util.Base64Utils; 39 import com.justhis.util.exception.UtilException; 40 41 import java.util.Collection; 42 43 import javax.jdo.JDOUserException; 44 import javax.jdo.PersistenceManager; 45 import javax.jdo.Query; 46 import javax.jdo.Transaction; 47 48 49 /*** 50 * ????Java Data Object???????? 51 * 52 * @author <a href="http://blog.ejb.cn">acai</a> 53 * @version $Revision: 1.1 $ 54 */ 55 public class JDOUtil { 56 //~ Static fields/initializers --------------------------------------------- 57 58 /*** jdo object id string */ 59 private static final Class[] ID_CONST_PARAM = { String.class }; 60 61 //~ Constructors ----------------------------------------------------------- 62 63 /*** 64 * Creates a new JDOUtil object. 65 */ 66 private JDOUtil() { 67 } 68 69 //~ Methods ---------------------------------------------------------------- 70 71 /*** 72 * create a persistence object into database by PersistenceManager. 73 * 74 * @param pm a persistence manager 75 * @param obj needed persistence object 76 */ 77 public static void createObject(PersistenceManager pm, Object obj) { 78 pm.currentTransaction().begin(); 79 pm.makePersistent(obj); 80 pm.currentTransaction().commit(); 81 } 82 83 /*** 84 * create a persistence object by transaction, through {@link 85 * #createObject(javax.jdo.PersistenceManager,java.lang.Object) 86 * createObject(pm,obj)} to persistence object in fact. 87 * 88 * @param transaction database transaction 89 * @param obj needed persistence object 90 * 91 * @see #createObject(javax.jdo.PersistenceManager,java.lang.Object) 92 */ 93 public static void createObject(Transaction transaction, Object obj) { 94 transaction.getPersistenceManager().makePersistent(obj); 95 } 96 97 /*** 98 * delete object from Persistence Manager . 99 * 100 * @param pm JDO persistence Manager. 101 * @param clazz a class name. 102 * @param objId persistence object id. 103 * 104 * @throws UtilException if delete from persistence manager occur any 105 * error. 106 */ 107 public static void deleteObject(PersistenceManager pm, Class clazz, 108 String objId 109 ) throws UtilException { 110 pm.currentTransaction().begin(); 111 112 Object deleteObj = findObjectById(pm, clazz, objId); 113 pm.deletePersistent(deleteObj); 114 115 pm.currentTransaction().commit(); 116 } 117 118 /*** 119 * delete JDO object from persistence manager 120 * 121 * @param transaction JDO transaction 122 * @param clazz an object class name 123 * @param objId JDO object id. 124 * 125 * @throws UtilException occur any error. 126 */ 127 public static void deleteObject(Transaction transaction, Class clazz, 128 String objId 129 ) throws UtilException { 130 PersistenceManager pm = transaction.getPersistenceManager(); 131 Object deleteObj = findObjectById(pm, clazz, objId); 132 pm.deletePersistent(deleteObj); 133 } 134 135 /*** 136 * find a jdo object list by class name. 137 * 138 * @param pm persistence manager 139 * @param cs class name 140 * 141 * @return collection of jdo object 142 * 143 * @throws UtilException if any exception occurs 144 */ 145 public static Collection findObjList(PersistenceManager pm, Class cs) 146 throws UtilException { 147 Query query = pm.newQuery(cs); 148 149 Collection list = (Collection) query.execute(); 150 151 return list; 152 } 153 154 /*** 155 * find jdo object list ordered 156 * 157 * @param pm persistence manager 158 * @param cs class name 159 * @param order order name 160 * 161 * @return a collection of jdo object 162 * 163 * @throws UtilException if any exception occurs. 164 */ 165 public static Collection findObjList(PersistenceManager pm, Class cs, 166 String order 167 ) throws UtilException { 168 Query query = pm.newQuery(cs); 169 query.setOrdering(order); 170 171 Collection list = (Collection) query.execute(); 172 173 return list; 174 } 175 176 /*** 177 * find collection of java data object list by filter and order. 178 * 179 * @param pm persistence manager 180 * @param cs JDO object class name 181 * @param order order name 182 * @param filter filter expression 183 * 184 * @return collection of java data object 185 * 186 * @throws UtilException if any exception occurs. 187 */ 188 public static Collection findObjList(PersistenceManager pm, Class cs, 189 String order, String filter 190 ) throws UtilException { 191 Query query = pm.newQuery(cs, filter); 192 193 if (order != null) { 194 query.setOrdering(order); 195 } 196 197 Collection list = (Collection) query.execute(); 198 199 return list; 200 } 201 202 /*** 203 * search JDO object from persistence manager by JDO transaction. 204 * 205 * @param transaction JDO transaction 206 * @param clazz a JDO object class name 207 * @param objId a JDO object id string 208 * 209 * @return an jdo object 210 * 211 * @throws UtilException if occur any error. 212 */ 213 public static Object findObjectById(Transaction transaction, Class clazz, 214 String objId 215 ) throws UtilException { 216 return findObjectById(transaction.getPersistenceManager(), clazz, objId); 217 } 218 219 /*** 220 * search JDO Object from persistence manager by JDO object ID . 221 * 222 * @param pm persistence manager 223 * @param clazz JDO object class name 224 * @param id a string of jdo object id 225 * 226 * @return a JDO object 227 * 228 * @throws UtilException if any exception occur 229 */ 230 public static Object findObjectById(PersistenceManager pm, Class clazz, 231 String id 232 ) throws UtilException { 233 Class idClazz = pm.getObjectIdClass(clazz); 234 String decoded; 235 236 try { 237 decoded = Base64Utils.decode(id.toCharArray()).toString(); 238 239 Object objId = idClazz.getConstructor(ID_CONST_PARAM).newInstance(new Object[] { 240 decoded 241 } 242 ); 243 Object result = pm.getObjectById(objId, false); 244 245 if (result != null) { 246 boolean ok; 247 248 ok = clazz.equals(result.getClass()); 249 250 if (!ok) { 251 throw new JDOUserException("Invalid id", id); 252 } 253 } 254 255 return result; 256 } catch (Exception e) { 257 throw new UtilException(e); 258 } 259 } 260 } 261 262 263 /* 264 * $Log: JDOUtil.java,v $ 265 * Revision 1.1 2003/09/10 09:22:14 bitiboy 266 * *** empty log message *** 267 * 268 * 269 */

This page was automatically generated by Maven